package com.qf.vote2203.consumer;

import com.qf.vote2203.dbvote.entity.Vote;
import com.qf.vote2203.esvo.VoteEsVo;
import com.qf.vote2203.repository.VoteInfoRepository;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class VoteInfoConsumer {

    @Autowired
    VoteInfoRepository voteInfoRepository;

    @RabbitListener(queues = "vote_queue")
    public void consumeMsg(List<Vote> votes, Message message, Channel channel){

        System.out.println(votes);
        // List<Vote> ---> List<VoteEsVo>
        final ArrayList<VoteEsVo> voteEsVos = new ArrayList<>();
        for (Vote vote : votes) {
            final VoteEsVo voteEsVo = new VoteEsVo();
            BeanUtils.copyProperties(vote,voteEsVo);
            voteEsVos.add(voteEsVo);
        }

        voteInfoRepository.saveAll(voteEsVos);

    }
}
